package com.tanhua.manager.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.manager.mapper.UserInfoMapper;
import com.tanhua.manager.pojo.User;
import com.tanhua.manager.pojo.UserInfo;
import com.tanhua.manager.pojo.Video;
import com.tanhua.manager.vo.PageResult;
import com.tanhua.manager.vo.UserPageVo;
import com.tanhua.manager.vo.VideoVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserVideoService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;

    @Autowired
    private UserInfoMapper userInfoMapper;

    public PageResult videoList(Integer page, Integer pagesize, String uid, String sortProp, String sortOrder) {
        //先通过uid查询用户详细信息，后面会用
        QueryWrapper<UserInfo> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("user_id", uid);
        UserInfo userInfo = userInfoMapper.selectOne(userQueryWrapper);
        //判断排序的字段
        if(sortProp.equals("createDate")){
            sortProp="created";
        }

        Query query = new Query(Criteria.where("userId").is(Long.valueOf(uid)));
        if(sortOrder.equals("descending")){
            query.with(Sort.by(Sort.Order.desc(sortProp)));
        }else if(sortOrder.equals("ascending")){
            query.with(Sort.by(Sort.Order.asc(sortProp)));
        }

        List<Video> videoList = mongoTemplate.find(query, Video.class);


        //创建返回的PageReultVo对象
        PageResult pageResult = new PageResult();
        pageResult.setPage(page); //页码
        pageResult.setCounts( videoList.size()); //总条目数
        pageResult.setPagesize(pagesize); //当前页条目数
        pageResult.setPages(videoList.size()/pagesize+1); //总页数

        //创建集合存储VideoVo对象
        List<VideoVo> userVideoVoList = new ArrayList<>();

        //因为返回的集合不能不设置，不然前端会报错
        if (CollectionUtils.isEmpty(videoList)) {
            pageResult.setItems(userVideoVoList);
            return pageResult;
        }


        for (Video video : videoList) {

            VideoVo videoVo = new VideoVo();

            videoVo.setId(video.getId().toHexString());
            videoVo.setNickname(userInfo.getNickName());
            videoVo.setUserId(userInfo.getUserId().intValue());
            videoVo.setCreateDate(video.getCreated().intValue());
            videoVo.setVideoUrl(video.getVideoUrl());
            videoVo.setPicUrl(video.getPicUrl());
            videoVo.setReportCount(0);      //举报数，设置默认0
            videoVo.setForwardingCount(0);  //转发数，设置默认0


            //获取点赞数
            String likeKey = "QUANZI_COMMENT_LIKE_" + videoVo.getId();
            String value = redisTemplate.opsForValue().get(likeKey);
            //判断点赞数value是否为空
            if (StringUtils.isEmpty(value)) {
                videoVo.setLikeCount(0);
            } else {
                videoVo.setLikeCount(Integer.parseInt(value));
            }

            //获取评论数
            Long commentCount = quanZiApi.queryCommentCount(videoVo.getId(), 2);
            videoVo.setCommentCount(commentCount.intValue());

            userVideoVoList.add(videoVo);
        }

        pageResult.setItems(userVideoVoList);
        return pageResult;
    }
}
